import time

MAX_TIME = 600
class CTimerManager(object):
    def __init__(self):
        self.m_Time = int(time.time()*100)
        self.m_Index = 0
        self.m_CommandList = []

        for i in range(MAX_TIME):
            self.m_CommandList.append([])

    def OnLoop(self):
        iNowTime = int(time.time()*100)
        iOldTime = self.m_Time
        self.m_Time = iNowTime
        iSubTime = iNowTime - iOldTime
        for iIndex in range(iSubTime):
            self.m_Index += 1
            if self.m_Index >= MAX_TIME:
                self.m_Index = 0
            lstCommand = self.m_CommandList[self.m_Index]
            self.m_CommandList[self.m_Index] = []
            for func in lstCommand:
                func()
    
    def AddCommand(self, func, iTimer):
        iCallTime = self.m_Index + iTimer
        iCallTime = iCallTime%MAX_TIME
        self.m_CommandList[iCallTime].append(func)

def Init():
    global g_TimerManager
    g_TimerManager = CTimerManager()

def GetTimerManager():
    global g_TimerManager
    return g_TimerManager

def Call_Out_Per_Second(func, iDealy):
    GetTimerManager().AddCommand(func, iDealy)